using System;
using System.IO;
using UnityEngine;
using UnityEngine.Recorder;

namespace UTJ.FrameCapturer.Recorders
{
	[Recorder(typeof(PNGRecorderSettings), "Video", "UTJ/Legacy/PNG")]
	[Obsolete("'UTJ/PNG' is obsolete, concider using 'Unity/Image Sequence' instead", false)]
	public class PNGRecorder : GenericRecorder<PNGRecorderSettings>
	{
		private fcAPI.fcPngContext m_ctx;

		public override bool BeginRecording(RecordingSession session)
		{
			if (!base.BeginRecording(session))
			{
				return false;
			}
			m_Settings.m_DestinationPath.CreateDirectory();
			m_ctx = fcAPI.fcPngCreateContext(ref m_Settings.m_PngEncoderSettings);
			return m_ctx;
		}

		public override void EndRecording(RecordingSession session)
		{
			m_ctx.Release();
			base.EndRecording(session);
		}

		public override void RecordFrame(RecordingSession session)
		{
			if (m_Inputs.Count != 1)
			{
				throw new Exception("Unsupported number of sources");
			}
			BaseRenderTextureInput baseRenderTextureInput = (BaseRenderTextureInput)m_Inputs[0];
			RenderTexture frame = baseRenderTextureInput.outputRT;
			string path2 = m_Settings.m_BaseFileName.BuildFileName(session, base.recordedFramesCount, frame.width, frame.height, "png");
			string path = Path.Combine(m_Settings.m_DestinationPath.GetFullPath(), path2);
			fcAPI.fcLock(frame, delegate(byte[] data, fcAPI.fcPixelFormat fmt)
			{
				fcAPI.fcPngExportPixels(m_ctx, path, data, frame.width, frame.height, fmt, 0);
			});
		}
	}
}
